package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.mapper.NoticeMapper;
import cn.tedu._03vehicle.pojo.entity.Notice;
import cn.tedu._03vehicle.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Slf4j
@EnableKnife4j
@Tag(name="03-资讯管理模块")
@RestController
@RequestMapping("/v1/notice")
public class NoticeController {

    @Autowired
    NoticeMapper noticeMapper;

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;




    @GetMapping("list")
    @Operation(summary = "查询资讯列表")
    @ApiOperationSupport(order = 30)
    public JsonResult list(){
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        Serializable redis_data = ops.get("vehicle_index");
        if(redis_data!=null){
            log.debug("从Redis中获取数据");
            return JsonResult.ok(redis_data);
        }
        List<NoticeListVO> noticeListVOS = noticeMapper.selectNoticeList();
        ops.set("vehicle_index",(Serializable) noticeListVOS ,30, TimeUnit.SECONDS);
        log.debug("数据在MYSQL数据库中获取");
        return JsonResult.ok(noticeListVOS);
    }
















}
